package C044;

import java.io.*;
import java.util.Arrays;

/**
 * ClassName: C03
 * Package: C043
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/5 18:11
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {
    public static int m, op;

    public static String[] splits;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        String line = null;

        while ((line = in.readLine()) != null) {
            m = Integer.valueOf(line);

            clear();
            for (int i = 1; i <= m; i++) {
                splits = in.readLine().split(" ");
                op = Integer.valueOf(splits[0]);
                if (op == 1) {
                    insert(splits[1]);
                } else if (op == 2) {
                    delete(splits[1]);
                } else if (op == 3) {
                    out.println(search(splits[1]) > 0 ? "YES" : "NO");
                    System.out.println(search(splits[1]) > 0 ? "YES" : "NO");
                } else if (op == 4) {
                    out.println(prefixNumber(splits[1]));
                    System.out.println(prefixNumber(splits[1]));
                }
            }
        }
        out.flush();
        in.close();
        out.close();
    }

    //
    private static int prefixNumber(String word) {
        char[] charArray = word.toCharArray();
        int index;
        int cur = 0;
        for (int i = 0; i < charArray.length; i++) {
            index = charArray[i] - 'a';
            if(arr[cur][index] == 0) {
                return 0;
            }
            cur = arr[cur][index];
        }
        return pass[cur];
    }

    // 查询元素 , 返回元素的个数
    private static int search(String word) {
        char[] charArray = word.toCharArray();
        int index;
        int cur = 0;
        for (int i = 0; i < charArray.length; i++) {
            index = charArray[i] - 'a';
            if(arr[cur][index] == 0) {
                return 0;
            }
            cur = arr[cur][index];
        }
        return end[cur];
    }

    // 删除关键字
    private static void delete(String word) {
        // 查询word是否存在

        char[] charArray = word.toCharArray();
        pass[0] --;
        int index;
        int cur = 0;
        for (int i = 0; i < charArray.length; i++) {
            index = charArray[i] - 'a';
            if (pass[arr[cur][index]] == 1) {
                arr[cur][index] = 0;
                return;
            }
            cur = arr[cur][index];
            pass[cur] --;
        }
        end[cur] --;
    }


    // 插入
    private static void insert(String word) {
        char[] charArr = word.toCharArray();
        pass[0] ++; // 最开头需要加1
        int cur = 0;
        int index ;
        for (int i = 0; i < charArr.length; i++) {
            index = charArr[i] - 'a';
            if(arr[cur][index] == 0) {
                arr[cur][index] = ++cnt;
            }
            cur = arr[cur][index];
            pass[cur] ++;
        }
        end[cur] ++;
    }

    private static void clear() {
        cnt = 0;
        for (int i = 0; i < arr.length; i++) {
            Arrays.fill(arr[i], 0);
            pass[i] = 0;
            end[i] = 0;
        }
    }

    static int MAXLEN = 100001;
    static int cnt = 0;
    static int[][] arr = new int[MAXLEN][26];

    static int[] pass = new int[MAXLEN];
    static int[] end = new int[MAXLEN];



}
